import time
import calendar

if __name__ == '__main__':
    # 分别获取三种形式的时间
    print(time.time())
    print(time.localtime())
    print(time.gmtime())

    time_stamp = time.time()
    # 时间戳转化为格式时间
    local_time = time.localtime(time_stamp)
    utc_time = time.gmtime(time_stamp)
    print(local_time, utc_time)

    # 格式时间转化回时间戳
    time_stamp_1 = time.mktime(local_time)
    time_stamp_2 = calendar.timegm(utc_time)
    print(time_stamp_1, time_stamp_2)

    # 各种时间形式和字符串之间的转换
    # 时间戳转字符串(本地时间字符串)
    print(time.ctime(time_stamp))  # Wed Jun 26 17:41:14 2019

    # 结构时间转字符串
    print(time.asctime(local_time))
    print(time.asctime(utc_time))

    # 结构时间转字符串:自定义格式
    print(time.strftime("%Y-%m-%d, %H:%M:%S, %w", local_time))
    print(time.strftime("%Y-%m-%d, %H:%M:%S", utc_time))

    # 字符串转回结构时间
    struct_time = time.strptime("2016-11-15, 15:32:12, 2", "%Y-%m-%d, %H:%M:%S, %w")
    print(struct_time)
